<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>资源环境管理测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        
        input, select, textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            box-sizing: border-box;
        }
        
        .form-row {
            display: flex;
            gap: 20px;
        }
        
        .form-row .form-group {
            flex: 1;
        }
        
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
        }
        
        button:hover {
            background-color: #0056b3;
        }
        
        button.secondary {
            background-color: #6c757d;
        }
        
        button.secondary:hover {
            background-color: #545b62;
        }
        
        .result {
            margin-top: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #007bff;
        }
        
        .result h3 {
            margin-top: 0;
            color: #333;
        }
        
        .result pre {
            background-color: white;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
            white-space: pre-wrap;
        }
        
        .error {
            border-left-color: #dc3545;
            background-color: #f8d7da;
        }
        
        .success {
            border-left-color: #28a745;
            background-color: #d4edda;
        }
        
        .loading {
            display: none;
            text-align: center;
            color: #666;
        }
        
        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #007bff;
            border-radius: 50%;
            width: 20px;
            height: 20px;
            animation: spin 1s linear infinite;
            display: inline-block;
            margin-right: 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>资源环境管理测试</h1>
        
        <form id="resourceForm">
            <div class="form-row">
                <div class="form-group">
                    <label for="resourceType">资源类型 *</label>
                    <select id="resourceType" name="resourceType" required>
                        <option value="">请选择资源类型</option>
                        <option value="virtualMachine">虚拟机</option>
                        <option value="container">容器</option>
                        <option value="bareMetal">裸金属</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="environmentType">环境类型 *</label>
                    <select id="environmentType" name="environmentType" required>
                        <option value="">请选择环境类型</option>
                        <option value="MySQL">MySQL</option>
                        <option value="Redis">Redis</option>
                        <option value="MongoDB">MongoDB</option>
                        <option value="PostgreSQL">PostgreSQL</option>
                        <option value="Elasticsearch">Elasticsearch</option>
                        <option value="Kafka">Kafka</option>
                        <option value="Nginx">Nginx</option>
                        <option value="Tomcat">Tomcat</option>
                        <option value="Docker">Docker</option>
                        <option value="Kubernetes">Kubernetes</option>
                    </select>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="systemEnvironment">系统环境 *</label>
                    <select id="systemEnvironment" name="systemEnvironment" required>
                        <option value="">请选择系统环境</option>
                        <option value="CentOS">CentOS</option>
                        <option value="Ubuntu">Ubuntu</option>
                        <option value="Debian">Debian</option>
                        <option value="RedHat">Red Hat</option>
                        <option value="Windows">Windows Server</option>
                        <option value="Alpine">Alpine</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="machineCount">机器数量 *</label>
                    <input type="number" id="machineCount" name="machineCount" min="1" max="100" value="1" required>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="cpu">CPU核心数 *</label>
                    <input type="number" id="cpu" name="cpu" min="1" max="64" value="2" required>
                </div>
                
                <div class="form-group">
                    <label for="memory">内存大小 (GB) *</label>
                    <input type="number" id="memory" name="memory" min="1" max="512" value="4" required>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="disk">硬盘大小 (GB) *</label>
                    <input type="number" id="disk" name="disk" min="10" max="10000" value="100" required>
                </div>
                
                <div class="form-group">
                    <label for="gpu">GPU数量</label>
                    <input type="number" id="gpu" name="gpu" min="0" max="16" value="0">
                </div>
            </div>
            
            <div class="form-group">
                <label for="description">描述信息</label>
                <textarea id="description" name="description" rows="3" placeholder="请输入环境描述信息..."></textarea>
            </div>
            
            <div class="form-group">
                <button type="submit">创建资源环境</button>
                <button type="button" class="secondary" onclick="getResourceTypes()">获取资源类型</button>
                <button type="button" class="secondary" onclick="getEnvironmentTypes()">获取环境类型</button>
                <button type="button" class="secondary" onclick="getSystemEnvironments()">获取系统环境</button>
            </div>
        </form>
        
        <div class="loading" id="loading">
            <div class="spinner"></div>
            正在处理请求...
        </div>
        
        <div class="result" id="result" style="display: none;">
            <h3>请求结果</h3>
            <pre id="resultContent"></pre>
        </div>
    </div>

    <script>
        const API_BASE = '/api/resource-environments';
        
        // 表单提交处理
        document.getElementById('resourceForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const requestData = {};
            
            for (let [key, value] of formData.entries()) {
                if (key === 'gpu' && value === '') {
                    requestData[key] = null;
                } else if (key === 'cpu' || key === 'memory' || key === 'disk' || key === 'gpu' || key === 'machineCount') {
                    requestData[key] = parseInt(value);
                } else {
                    requestData[key] = value;
                }
            }
            
            await createResourceEnvironment(requestData);
        });
        
        // 创建资源环境
        async function createResourceEnvironment(data) {
            showLoading(true);
            hideResult();
            
            try {
                const response = await fetch(`${API_BASE}/create`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (response.ok) {
                    showResult(result, 'success');
                } else {
                    showResult(result, 'error');
                }
                
            } catch (error) {
                showResult({
                    success: false,
                    message: '请求失败: ' + error.message
                }, 'error');
            } finally {
                showLoading(false);
            }
        }
        
        // 获取资源类型列表
        async function getResourceTypes() {
            await fetchAndShowResult(`${API_BASE}/resource-types`, '获取资源类型');
        }
        
        // 获取环境类型列表
        async function getEnvironmentTypes() {
            await fetchAndShowResult(`${API_BASE}/environment-types`, '获取环境类型');
        }
        
        // 获取系统环境列表
        async function getSystemEnvironments() {
            await fetchAndShowResult(`${API_BASE}/system-environments`, '获取系统环境');
        }
        
        // 通用获取数据方法
        async function fetchAndShowResult(url, title) {
            showLoading(true);
            hideResult();
            
            try {
                const response = await fetch(url);
                const result = await response.json();
                
                if (response.ok) {
                    showResult(result, 'success');
                } else {
                    showResult(result, 'error');
                }
                
            } catch (error) {
                showResult({
                    success: false,
                    message: '请求失败: ' + error.message
                }, 'error');
            } finally {
                showLoading(false);
            }
        }
        
        // 显示加载状态
        function showLoading(show) {
            document.getElementById('loading').style.display = show ? 'block' : 'none';
        }
        
        // 显示结果
        function showResult(data, type) {
            const resultDiv = document.getElementById('result');
            const resultContent = document.getElementById('resultContent');
            
            resultContent.textContent = JSON.stringify(data, null, 2);
            resultDiv.className = `result ${type}`;
            resultDiv.style.display = 'block';
        }
        
        // 隐藏结果
        function hideResult() {
            document.getElementById('result').style.display = 'none';
        }
    </script>
</body>
</html> 